/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.thienloc.omap.controller.report;

import com.thienloc.omap.controller.BaseBean;
import com.thienloc.omap.jdbc.entity.SubCpProduction;
import com.thienloc.omap.session.TblSubCpFacade;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

/**
 *
 * @author GiangLT
 */
@ManagedBean
@ViewScoped
public class SubCpProductionBean extends BaseBean implements Serializable {

    @EJB
    private TblSubCpFacade subCpFacade;
    private Integer month;
    private Integer year;
    private String subCp;
    private List<SubCpProduction> viettelList = new ArrayList<SubCpProduction>();
    private List<SubCpProduction> vinaphoneList = new ArrayList<SubCpProduction>();
    private List<SubCpProduction> mobifoneList = new ArrayList<SubCpProduction>();
    private List<String> operators;
    private final Logger logger = LoggerFactory.getLogger(SubCpProductionBean.class);

    public void report() {
        try {
            viettelList = new ArrayList<SubCpProduction>();
            vinaphoneList = new ArrayList<SubCpProduction>();
            mobifoneList = new ArrayList<SubCpProduction>();
            List<String> subCps = new ArrayList<String>();
            subCps.add(subCp);
            List<SubCpProduction> subCpProductions = subCpFacade.countByTime(month, year, subCps, operators);

            Comparator<SubCpProduction> comparator = new Comparator<SubCpProduction>() {

                @Override
                public int compare(SubCpProduction o1, SubCpProduction o2) {
                    return o1.getDate().compareTo(o2.getDate());
                }
            };

            for (SubCpProduction subCpProduction : subCpProductions) {
                String operator = subCpProduction.getOperator();
                if (operator.equals("smsgate_viettel6771")) {
                    viettelList.add(subCpProduction);
                } else if (operator.equals("smsgate_vinaphone6771")) {
                    vinaphoneList.add(subCpProduction);
                } else if (operator.equals("smsgate_mobifone6771")) {
                    mobifoneList.add(subCpProduction);
                }
            }

            Collections.sort(viettelList, comparator);
            Collections.sort(vinaphoneList, comparator);
            Collections.sort(mobifoneList, comparator);
            addTotalRow(viettelList);
            addTotalRow(vinaphoneList);
            addTotalRow(mobifoneList);
        } catch (Exception ex) {
            logger.error("[{}]: ", getUsername(), ex);
        }
    }

    private void addTotalRow(List<SubCpProduction> list) {
        SubCpProduction totalRow = new SubCpProduction();
        totalRow.setDateString("Total");
        totalRow.setMo6071(0);
        totalRow.setMo6171(0);
        totalRow.setMo6271(0);
        totalRow.setMo6371(0);
        totalRow.setMo6471(0);
        totalRow.setMo6571(0);
        totalRow.setMo6671(0);
        totalRow.setMo6771(0);

        totalRow.setMt6071(0);
        totalRow.setMt6171(0);
        totalRow.setMt6271(0);
        totalRow.setMt6371(0);
        totalRow.setMt6471(0);
        totalRow.setMt6571(0);
        totalRow.setMt6671(0);
        totalRow.setMt6771(0);

        for (SubCpProduction subCpProduction : list) {
            totalRow.setMo6071(totalRow.getMo6071() + subCpProduction.getMo6071());
            totalRow.setMo6171(totalRow.getMo6171() + subCpProduction.getMo6171());
            totalRow.setMo6271(totalRow.getMo6271() + subCpProduction.getMo6271());
            totalRow.setMo6371(totalRow.getMo6371() + subCpProduction.getMo6371());
            totalRow.setMo6471(totalRow.getMo6471() + subCpProduction.getMo6471());
            totalRow.setMo6571(totalRow.getMo6571() + subCpProduction.getMo6571());
            totalRow.setMo6671(totalRow.getMo6671() + subCpProduction.getMo6671());
            totalRow.setMo6771(totalRow.getMo6771() + subCpProduction.getMo6771());

            totalRow.setMt6071(totalRow.getMt6071() + subCpProduction.getMt6071());
            totalRow.setMt6171(totalRow.getMt6171() + subCpProduction.getMt6171());
            totalRow.setMt6271(totalRow.getMt6271() + subCpProduction.getMt6271());
            totalRow.setMt6371(totalRow.getMt6371() + subCpProduction.getMt6371());
            totalRow.setMt6471(totalRow.getMt6471() + subCpProduction.getMt6471());
            totalRow.setMt6571(totalRow.getMt6571() + subCpProduction.getMt6571());
            totalRow.setMt6671(totalRow.getMt6671() + subCpProduction.getMt6671());
            totalRow.setMt6771(totalRow.getMt6771() + subCpProduction.getMt6771());
        }
        list.add(totalRow);
    }

    public String getSubCp() {
        return subCp;
    }

    public void setSubCp(String subCp) {
        this.subCp = subCp;
    }

    public List<SubCpProduction> getMobifoneList() {
        return mobifoneList;
    }

    public List<SubCpProduction> getViettelList() {
        return viettelList;
    }

    public List<SubCpProduction> getVinaphoneList() {
        return vinaphoneList;
    }

    public Integer getMonth() {
        return month;
    }

    public void setMonth(Integer month) {
        this.month = month;
    }

    public Integer getYear() {
        return year;
    }

    public void setYear(Integer year) {
        this.year = year;
    }

    public List<String> getOperators() {
        return operators;
    }

    public void setOperators(List<String> operators) {
        this.operators = operators;
    }
}
